package cn.hl.hm.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jibx.runtime.IBindingFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.hl.hm.constants.ModelConfig;
import cn.hl.hm.constants.UploadConfig;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.DriverVehicle;
import cn.hl.hm.entity.RoadFee;
import cn.hl.hm.service.DriverService;
import cn.hl.hm.service.DriverVehicleService;
import cn.hl.hm.util.BeanPicUtil;
import cn.hl.hm.util.EmailUtil;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.OSSUtil;
import cn.hl.hm.util.QRCodeUtil;
import cn.hl.hm.util.Result;

@Controller
@RequestMapping("/aliyun")
public class ALiYunPictureController {

	private static final Logger logger = LoggerFactory.getLogger(ALiYunPictureController.class);
	@Autowired
	private DriverService driverService;
	
	@Autowired
	private DriverVehicleService driverVehicleService;
	
	@Autowired
	private BeanPicUtil beanPicUtil;
	
	@Autowired
	private OSSUtil ossutil;
	
	@Autowired
	@Qualifier("uploadConfig")
	private UploadConfig uploadConfig;
	
	@RequestMapping(value = "/transferpic", method = RequestMethod.POST)
	public String transferpic( HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Driver driver =new Driver();
			List<Driver> selectDriver = driverService.selectDriver(driver);
			int i = 1;
			long currentTimeMillis = System.currentTimeMillis();
			for (Driver driver2 : selectDriver) {
				long currentTimeMillis3 = System.currentTimeMillis();
				//司机头像
				String headPic = driver2.getHeadPic();
				if(headPic!=null&&!headPic.equals("")) {
					headPic=uploadConfig.getShortUrl()+headPic;
					File file=new File(headPic);
					String name = file.getName();
					 String prefix=name.substring(name.lastIndexOf(".")+1);
					String filename = "hm/"+ "driverHeadpic"+System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000)+"."+prefix;
					if(file.exists()) {
					ossutil.putObject(filename, file);
					driver2.setHeadPic(filename);
					}
				}
				//司机身份证Back
				String idCardBackPic = driver2.getIdCardBackPic();
				if(idCardBackPic!=null&&!idCardBackPic.equals("")) {
					idCardBackPic=uploadConfig.getShortUrl()+idCardBackPic;
					File file=new File(idCardBackPic);
					String name = file.getName();
					 String prefix=name.substring(name.lastIndexOf(".")+1);
					String filename = "hm/"+ "driverIdcardBack"+System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000)+"."+prefix;
					if(file.exists()) {
					ossutil.putObject(filename, file);
					driver2.setIdCardBackPic(filename);
					}
				}
				//司机身份证front
				String idCardFrontPic = driver2.getIdCardFrontPic();
				if(idCardFrontPic!=null&&!idCardFrontPic.equals("")) {
					idCardFrontPic=uploadConfig.getShortUrl()+idCardFrontPic;
					File file=new File(idCardFrontPic);
					String name = file.getName();
					 String prefix=name.substring(name.lastIndexOf(".")+1);
					String filename = "hm/"+ "driverIdcardFront"+System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000)+"."+prefix;
					if(file.exists()) {
					ossutil.putObject(filename, file);
					driver2.setIdCardFrontPic(filename);
					}
				}
				
				//司机gua照片
				String drivingGuaPic2 = driver2.getDrivingGuaPic();
				
				if(drivingGuaPic2!=null&&!drivingGuaPic2.equals("")) {
					drivingGuaPic2=uploadConfig.getShortUrl()+drivingGuaPic2;
					File file=new File(drivingGuaPic2);
					String name = file.getName();
					 String prefix=name.substring(name.lastIndexOf(".")+1);
					String filename = "hm/"+ "driverGua"+System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000)+"."+prefix;
					if(file.exists()) {
					ossutil.putObject(filename, file);
					driver2.setDrivingGuaPic(filename);
					}
					
				}
				//drivingLicense图片
				String drivingLicensePic2 = driver2.getDrivingLicensePic();
				if(drivingLicensePic2!=null&&!drivingLicensePic2.equals("")) {
					drivingLicensePic2=uploadConfig.getShortUrl()+drivingLicensePic2;
					File file=new File(drivingLicensePic2);
					String name = file.getName();
					 String prefix=name.substring(name.lastIndexOf(".")+1);
					String filename = "hm/"+ "drivingLicense"+System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000)+"."+prefix;
					if(file.exists()) {
					ossutil.putObject(filename, file);
					driver2.setDrivingLicensePic(filename);
					}
				}
				
				//driverLicensePic图片
				String driverLicensePic = driver2.getDriverLicensePic();
				if(driverLicensePic!=null&&!driverLicensePic.equals("")) {
					driverLicensePic=uploadConfig.getShortUrl()+driverLicensePic;
					File file=new File(driverLicensePic);
					String name = file.getName();
					 String prefix=name.substring(name.lastIndexOf(".")+1);
					String filename = "hm/"+ "driverLicense"+System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000)+"."+prefix;
					if(file.exists()) {
					ossutil.putObject(filename, file);
					driver2.setDriverLicensePic(filename);
					}
				}
				//qualificationCertificatePic图片
				String qualificationCertificatePic = driver2.getQualificationCertificatePic();
				if(qualificationCertificatePic!=null&&!qualificationCertificatePic.equals("")) {
					qualificationCertificatePic=uploadConfig.getShortUrl()+qualificationCertificatePic;
					File file=new File(qualificationCertificatePic);
					String name = file.getName();
					 String prefix=name.substring(name.lastIndexOf(".")+1);
					String filename = "hm/"+ "qualificationCertificate"+System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000)+"."+prefix;
					if(file.exists()) {
					ossutil.putObject(filename, file);
					driver2.setQualificationCertificatePic(filename);
					}
				}
				
				//司机二维码
				String qrInfo = driver2.getQrInfo();
				if(qrInfo!=null&&!qrInfo.equals("")) {
					qrInfo=	qrInfo.replace("http://hm.hlvan.cn/upload/",uploadConfig.getShortUrl());
					File file=new File(qrInfo);
					String name = file.getName();
					 String prefix=name.substring(name.lastIndexOf(".")+1);
					String filename = "hm/qrInfo/"+ "qr"+System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000)+"."+prefix;
					if(file.exists()) {
					ossutil.putObject(filename, file);
					driver2.setQrInfo(filename);
					}
				}
				
			
				
				driverService.updateByPrimaryKeySelective(driver2);
				
				List<DriverVehicle> driverVehicleList=driverVehicleService.selectByDriverId(driver2.getId());
				for (DriverVehicle driverVehicle : driverVehicleList) {
					//汽车License图片
					String drivingLicensePic = driverVehicle.getDrivingLicensePic();
					if(drivingLicensePic!=null&&!drivingLicensePic.equals("")) {
						drivingLicensePic=uploadConfig.getShortUrl()+drivingLicensePic;
						File file=new File(drivingLicensePic);
						String name = file.getName();
						 String prefix=name.substring(name.lastIndexOf(".")+1);
						String filename = "hm/"+ "vehicleLicense"+System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000)+"."+prefix;
						if(file.exists()) {
						ossutil.putObject(filename, file);
						driverVehicle.setDrivingLicensePic(filename);
						}
					}
					//汽车Gua图片
					String drivingGuaPic = driverVehicle.getDrivingGuaPic();
					if(drivingGuaPic!=null&&!drivingGuaPic.equals("")) {
						drivingGuaPic=uploadConfig.getShortUrl()+drivingGuaPic;
						File file=new File(drivingGuaPic);
						String name = file.getName();
						 String prefix=name.substring(name.lastIndexOf(".")+1);
						String filename = "hm/"+ "vehicleGua"+System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000)+"."+prefix;
						if(file.exists()) {
						ossutil.putObject(filename, file);
						driverVehicle.setDrivingGuaPic(filename);
						}
						
					}
					//汽车车牌号图片
					String plateNumberPic = driverVehicle.getPlateNumberPic();
					if(plateNumberPic!=null&&!plateNumberPic.equals("")) {
						plateNumberPic=uploadConfig.getShortUrl()+plateNumberPic;
						File file=new File(plateNumberPic);
						String name = file.getName();
						 String prefix=name.substring(name.lastIndexOf(".")+1);
						String filename = "hm/"+ "vehiclePlateNumber"+System.currentTimeMillis() + (int) ((Math.random() * 9 + 1) * 1000)+"."+prefix;
						if(file.exists()) {
						ossutil.putObject(filename, file);
						driverVehicle.setPlateNumberPic(filename);
						}
					}
					
					driverVehicleService.updateByPrimaryKeySelective(driverVehicle);
					
					
				}
				long currentTimeMillis4 = System.currentTimeMillis();
				logger.info("执行到了" +i+"/"+selectDriver.size()+"执行了"+(currentTimeMillis4-currentTimeMillis3)+"毫秒");
				i++;
			}
			long currentTimeMillis2 = System.currentTimeMillis();
			logger.info("总共执行了"+(currentTimeMillis2-currentTimeMillis)/1000+"秒");
			
		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.FAILURE, response);
		}
		return null;
	}
}
